/*
 * The MIT License
 *
 * Copyright 2014 David Rodenas Pico.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package edu.uoc.sd.fr14.model;

import java.io.Serializable;

/**
 * This is an identifier of a non-commited op id.
 * <p>
 * 
 * When the op is commited it will obtain a {@link Nseq}, while
 * it is not commited it will referenced only as {@link OpId}.
 * <p>
 * 
 * To obtain a OpId you should annotate it with {@link Log#annotate(FlightId, CustomerId, ProxyId)},
 * it will have an {@link Op} associated until {@link Log#executeOp(Op, Company)} executes it
 * correctly (in fact {@link Log#prepared(OpId, Quorum)} is who recovers it), or until
 * {@link Log#cancelOps()} is called, in which case will be cancelled and destroyend and
 * never will obtain an NSeq.
 * 
 * 
 * @author drodenas
 */
public final class OpId implements Serializable,Comparable<OpId> {    
	private static final long serialVersionUID = 901727115807807152L;
	protected OpId() { id=0; }
	
	private final int id;

    public OpId(int id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 47 * hash + this.id;
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final OpId other = (OpId) obj;
        return this.id == other.id;
    }

    @Override
    public int compareTo(OpId o) {
        return this.id - o.id;
    }

    @Override
    public String toString() {
        return "Op{" + id + '}';
    }
    
}
